package org.aplikator.client.local.widgets;

import com.github.gwtbootstrap.datepicker.client.ui.DateBox;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import org.aplikator.client.local.Aplikator.AplikatorResources;
import org.aplikator.client.shared.descriptor.PropertyDTO;

import java.util.Date;

public class DateFieldWidget extends DataFieldBase implements DataField<Date> {

	private DateBox box = new DateBox();


    public DateFieldWidget(String caption, PropertyDTO property, int size, String formatPattern) {
        super(caption, property);
        controls.add(box);
        if (formatPattern!= null){
            box.setFormat(formatPattern);
        }
        box.addValueChangeHandler(new ValueChangeHandler<Date>() {
            public void onValueChange(ValueChangeEvent<Date> event) {
                setDirty(true);
            }
        });
        if (size>0) {
            box.setSize(size);
        }
    }

    public void setValue(Date value) {
        box.setValue(value);
    }

    public Date getValue() {
        return box.getValue();
    }

    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
        return box.addValueChangeHandler(handler);
    }

    public void setEnabled(boolean enabled) {
        box.setEnabled(enabled);
    }

    public void setDirty(boolean dirty) {
        super.setDirty(dirty);
        if (dirty) {
            box.addStyleName(AplikatorResources.INSTANCE.css().dirty());
        } else {
            box.removeStyleName(AplikatorResources.INSTANCE.css().dirty());
        }
    }

}
